Polymorphism refers to an object ability to have on multiple forms.
Polymorphism in Java refers to a class's ability to provide different implementations of a method depending on the type of object passed to the method.
In simple word polymorphism in Java allows us to perform the same action in a different ways.
Polymorphism in Java refers to any Java object that can pass more than one IS-A test.
As a result, all Java objects are polymorphic because they passed the IS-A test for their own type and the class Object.
// Java polymorphism example
class GrandParent { void print() { System.out.println("Hi I am GrandFather"); } } class Parent extends GrandParent{ void print() { System.out.println("Hi I am Father"); } } class Child extends Parent { void print() { System.out.println("Hi I am Child"); } } public class Main { void declaration(String name) { System.out.println("Hi myself " + name); } void declaration(String fname, String lname) { System.out.println("Hi myself " + fname + " " + lname); } public static void main(String[] args) { // GrandParent class instance System.out.println("---------------Run-time polymorphism---------------"); GrandParent grandParent ; grandParent = new GrandParent(); grandParent.print(); grandParent = new Parent(); grandParent.print(); grandParent = new Child(); grandParent.print(); System.out.println("---------------Complile time ploymorphism---------------"); Main main = new Main(); main.declaration("Alok."); main.declaration("Alok", "Raja."); } }
Output:
---------------Run-time polymorphism---------------
Hi I am GrandFather
Hi I am Father
Hi I am Child
---------------Complile time ploymorphism---------------
Hi myself Alok.
Hi myself Alok Raja.
Method overloading is the process of creating multiple methods with the same name in the same class, each of which works differently. Method overloading is a situation when a class contains multiple methods with the same name but having different papameter and return type.
// Java program with method overloading
class Product { int Multiply(int p, int q){ return p * q; } float Multiply(float p, float q){ return p * q; } } class Overloading { public static void main(String[] args){ Product product=new Product(); System.out.println(product.Multiply(2, 4)); System.out.println(product.Multiply(5.2f, 6.3f)); } }
Output:
8
32.76
Method overriding occurs when a subclass or child class declares the same method as the parent class.
// Java program with Method Overriding
class Vehicle{ void run(){ System.out.println("Vehicle is moving"); } } class Bike extends Vehicle{ void run(){ System.out.println("Bike is running safely"); } public static void main(String args[]){ Bike obj = new Bike(); obj.run(); } }
Output:
Bike is running safely
Compile Time Polymorphism Static Polymorphism is another name for it in Java. Furthermore, the method call is resolved at compile time. Method Overloading is used to achieve compile-time polymorphism. Operator Overloading can also be used to achieve this type of polymorphism. However Java does not support Operator Overloading.
Method overloading does when a class has multiple methods with the same name, but the number, types, and order of parameters, as well as the return type, different. Java allows users to use the same name for multiple functions as long as they can be distinguished by the type and number of parameters.
// Java program using Compile time Polymorphism
class Product { void Multiply(int p, int q){ System.out.println("Addition of two numbers : " +(p+q)); } void Multiply(int p, int q,int r){ System.out.println("Addition of three numbers : " +(p+q+r)); } } class Overloading { public static void main(String[] args){ Product product=new Product(); product.Multiply(2, 4); product.Multiply(10, 20,30); } }
Output:
Addition of two numbers : 6
Addition of three numbers : 60
The sum() method in this programme is overloaded with two types via different parameters.This is the baisc idea behind compile-time polymorphism in Java, where we can perform different operations by calling multiple methods with the same name.
// Java program Runtime Polymorphism
class car { int speedlimit = 25; void run() { System.out.println("Runnning at 25 kmph speed"); } } class Nano extends car { int speedlimit = 35; void run() { System.out.println("Runnning at 35 kmph speed"); } public static void main(String args[]) { car obj = new Nano(); System.out.println(obj.speedlimit); obj.run(); } }
Output:
25 // run time polymorphism achive
Runnning at 35 kmph speed
Post your comment